package com.xuanmiao.smsback.web.controller.operator;

import com.xuanmiao.smsback.domain.qo.RechargeManageInfoQO;
import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;

import com.xuanmiao.smsback.model.UserInfoBO;
import com.xuanmiao.smsback.web.controller.vo.RechargeRecordInfoVO;
import com.xuanmiao.smsback.model.RechargeRecordInfoBO;
import com.xuanmiao.smsback.service.platform.operator.RechargeManageInfoService;

import com.xuanmiao.smsback.web.controller.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Api("商户管理")
@RequestMapping(value = "/RechargeRecordInfo")
@RestController
public class RechargeManageInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private RechargeManageInfoService rechargeManageInfoService;

    @ApiOperation("充值管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimeStart" ,value = "创建时间开始" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd" ,value = "创建时间结束" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createType" ,value = "创建方式0-系统自动 1-手动" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "account" ,value = "账户" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "businessNo" ,value = "商户号" ,dataType = "String",paramType = "query"),

    })
    @GetMapping()
    BasicResult<List<RechargeRecordInfoVO>> all(@ApiIgnore RechargeManageInfoQO rechargeManageInfoQO){

        try {
            List<RechargeRecordInfoBO> listBOs= rechargeManageInfoService.getList(rechargeManageInfoQO);
            List<RechargeRecordInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, RechargeRecordInfoVO.class);
                listVOs.stream().forEach(listVO -> {
                    if (listVO.getEndTime() != null) {
                        listVO.setEndTimeStr(DateFormatUtils.format(listVO.getEndTime(), "yyyy/MM/dd HH:mm"));
                    }
                    listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));
                    if (listVO.getModifyTime()!= null) {
                        listVO.setModifyTimeStr(DateFormatUtils.format(listVO.getModifyTime(), "yyyy/MM/dd HH:mm"));
                    }
                });
            }
            return BasicResult.ok(listVOs,rechargeManageInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("商户号是否存在 返回boolean true-存在 通过 false-不存在")
    @GetMapping("/checkBusinessNoExist/{businessNo}")
    BasicResult<Boolean> checkBusinessNo(@PathVariable("businessNo") String businessNo) {
        try {
            Boolean exist = rechargeManageInfoService.existBusinessNo(businessNo);
            return BasicResult.ok(exist);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), BasicResultCode.QUERY_FAIL.getMessage());
        }
    }
    @ApiOperation("订单号是否重复 返回boolean true-重复 不通过  false-不存在")
    @GetMapping("/checkTransactionIdExist/{transactionId}")
    BasicResult<Boolean> checkTransactionId(@PathVariable("transactionId") String transactionId) {
        try {
            Boolean exist = rechargeManageInfoService.existTransactionId(transactionId);
            return BasicResult.ok(exist);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), BasicResultCode.QUERY_FAIL.getMessage());
        }
    }

    @ApiOperation("新增充值")
    @PostMapping("/create")
    BasicResult<Void> create( @RequestBody RechargeRecordInfoVO rechargeRecordInfoVO) {
        RechargeRecordInfoBO rechargeRecordInfoBO=DozerUtils.convert(rechargeRecordInfoVO, RechargeRecordInfoBO.class);

        if (!rechargeManageInfoService.existBusinessNo(rechargeRecordInfoBO.getBusinessNo())){
            throw new RuntimeException("没有该商户号");
        }
        if (rechargeManageInfoService.existTransactionId(rechargeRecordInfoBO.getTransactionId())){
            throw new RuntimeException("交易单号重复");
        }
       try {
           rechargeManageInfoService.add(rechargeRecordInfoBO);
           return BasicResult.ok();
       } catch (Exception e) {
           LOGGER.error(e.getMessage(), e);
           return BasicResult.error(BasicResultCode. ADD_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode. ADD_FAIL.getMessage()));
       }
    }
    @ApiOperation("主键查询充值信息")
    @GetMapping("{id:\\d+}")
    BasicResult<RechargeRecordInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            RechargeRecordInfoBO res = rechargeManageInfoService.getById(id);
            return BasicResult.ok(DozerUtils.convert(res, RechargeRecordInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("充值信息更新")
    @PutMapping("/update")
    BasicResult<Void> update(@Valid @RequestBody RechargeRecordInfoVO rechargeRecordInfoVO) {
        try {
            rechargeManageInfoService.update(DozerUtils.convert(rechargeRecordInfoVO, RechargeRecordInfoBO.class));
            return BasicResult.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(), BasicResultCode.UPDATE_FAIL.getMessage());
        }
    }
}
